package com.zhangck.bos2_0.Service.system.Impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zhangck.bos2_0.Service.system.UserServices;
import com.zhangck.bos2_0.dao.system.RoleRepository;
import com.zhangck.bos2_0.dao.system.UserRepository;

import cn.itcast.bos.domain.system.Role;
import cn.itcast.bos.domain.system.User;

/** 
* @author 上午茶
* @version 创建时间：2018年6月16日 下午6:46:32 
* 类说明 
*/
@Service
@Transactional
public class UserServicesImpl implements UserServices{

	@Autowired
	private UserRepository userRepository;
	
	@Autowired
	private RoleRepository roleRepository;
	
	@Override
	public User findByUsername(String username) {
		return userRepository.findByUsername(username);
	}

	@Override
	public List<User> findAll() {
		return userRepository.findAll();
	}

	@Override
	public void saveUser(User model, String[] roleIds) {
		if (roleIds!=null) {
			for (String string : roleIds) {
				Role role = roleRepository.findOne(Integer.parseInt(string));
				model.getRoles().add(role);
			}
		}
		userRepository.save(model);
	}

}
